/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

define(['adminapp'], function ($app) {
    $app.navactive('dining_record');



    var $listtable = $app.listtable({
        'sAjaxSource'   : g.ctx + "admin/booking/dining/filter",
        'bSort'         : true,
        'bAutoWidth'    : true,
        'fnServerParams': function (aoData) {
            var sd = $('#search_form').serializeJSON();
            $.each(sd, function (name, val) {
                aoData.push({"name": name, "value": val});
            });
        },
        'aoColumns'     : [
            $app.checkall,
            $app.autono,
            {'mDataProp': 'name', 'bSortable': false},
            {'mDataProp': 'phone', 'bSortable': false},
            {'mDataProp': 'brand_name' },
            {'mDataProp': 'booking_date'},
            {'mDataProp': 'market_name'},
            {'mDataProp': 'booking_pos', 'bSortable': false},
            {'mDataProp': 'meal_num', 'bSortable': false},
            {'mDataProp': 'table_name', 'bSortable': false},
            {'mDataProp': 'status' },
            {'mDataProp': 'use_time' },
            {'mDataProp': 'voucher', 'bSortable': false}
        ],
        'aoColumnDefs'  : [
            {
                'aTargets': [10],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    if (full['status'] == 1) {
                        return '未到店';
                    } else if (full['status'] == 2) {
                        return '已到店';
                    } else {
                        return '过期';
                    }
                }
            }
        ]
    }, 'data-list');

    var $brand_filter = $('#brand-filter');
    $brand_filter.on('change', function (e) {
        var $filter_market = $('#filter_market');
        var $filter_category = $('#filter_category');
        $filter_market.find('option:gt(0)').remove();
        $filter_category.find('option:gt(0)').remove();
        $.get(g.ctx + 'admin/booking/dining/brand_marker_category/' + $(this).val(), function (rst) {
            if (rst.status == 'OK') {
                var data = rst.data;
                var category = data['category'];
                var market = data['market'];
                var category_options = [];
                for (var i = 0; i < category.length; i++) {
                    var _data = category[i];
                    category_options.push({id: _data['id'], text: _data['name'] })
                }
                var market_options = [];
                for (var j = 0; j < market.length; j++) {
                    var _data_m = market[j];
                    market_options.push({id: _data_m['id'], text: _data_m['name'] })
                }

                $filter_category.append($app.selectOption(category_options)).select2();
                $filter_market.append($app.selectOption(market_options)).select2()
            }
        })
    });

    $('#search_btn').on('click', function (e) {
        e.preventDefault();
        $listtable.fnReloadAjax();
    });
});